name = odeds
main = $(name).pdf
bibfile = $(name).bib
include .makevar

$(main): $(files) #$(name).bbl
	xelatex -halt-on-error $(name).tex  |egrep "Warning|Error|Output|^!|^l."
#	touch $(name).bbl

#$(name).bbl: $(bibfile) $(name).aux
#	bibtex $(name).aux

#$(name).aux: $(files)
#	pdflatex -halt-on-error $(name).tex |egrep "Error|^!|^l.|^Output"  

.makevar: $(name).tex
	echo -n 'files = $(name).tex '> $@
	sed -n -e 's/^%\?\\input{\([^{} \.]\+\)\(\.tex\)\?}/\1.tex/p' $(name).tex |tr '\n' ' ' >> $@

.labels: $(files)
	sed -n -e 's/^[^%]*\\label{\([a-zA-Z0-9:'\'' -]*\)}.*$$/"\1"/p' $^ |sort > .labels     # attention  double dollar sign passes a single dollar sign to shell

.cites:  $(bibfile)
	sed -n -e 's/^[[:blank:]]*@[[:alpha:]]\+{\([-a-zA-Z0-9]\+\),$$/\1/p' $^ |sort |tr '\n' ' ' > .cites


#.commands: $(name).tex
#	echo -n '<!--MAKE GENERATED HERE--><match>\\\\(' > .commands
#	sed -n -e 's/^[^%]*\\\(\(re\)\?newcommand \?\|DeclareMathOperator\){\\\([a-zA-Z0-9]\+\)}.*$$/\3/p' $(name).tex|tr '\n' '|' | sed -e 's/|$$/)(?=\\b|_)<\/match>\n/' >> .commands

# update highlight 
# after 'make update-hl' need to restart gedit
#lang = ~/.local/share/gtksourceview-4/language-specs/latex.lang
#.PHONY: update-hl 
#update-hl: .commands
#	sed -i -e '/^<!--MAKE GENERATED HERE-->/r .commands' -e '/^<!--MAKE GENERATED HERE-->/{d;q}' $(lang) 

.PHONY: tar
tar:  $(name).tar.gz

$(name).tar.gz: $(files) $(bibfile) Makefile
	tar -czf $@ $^

.PHONY: untar
untar:
	tar -xzf $(name).tar.gz

.PHONY: clean
clean:
	rm -f $(name).toc $(files:.tex=.aux) $(files:.tex=.out) $(files:.tex=.log) $(files:.tex=.bbl) $(files:.tex=.blg) .makevar .cites .labels

.PHONY: show
show:
	evince $(main) &

.PHONY: edit
edit:
	nvim $(name).tex
#edit: update-hl
#	gedit $(name).tex &

.PHONY: again
again:
	xelatex -halt-on-error $(name).tex

